package com.atguigu.kafka.producer;

import org.apache.kafka.clients.producer.KafkaProducer;
import org.apache.kafka.clients.producer.ProducerConfig;
import org.apache.kafka.clients.producer.ProducerRecord;
import org.apache.kafka.common.serialization.StringSerializer;

import java.util.Properties;
import java.util.concurrent.ExecutionException;

/**
 * @author: 叶子
 * @date: 2022/08/14 17:14
 * 异步的send方法后面加 .get()就是同步发送
 **/
public class CustomProducerSync {
    public static void main(String[] args) throws ExecutionException, InterruptedException {
        //0 配置
        Properties properties = new Properties();
        //连接集群
        properties.put(ProducerConfig.BOOTSTRAP_SERVERS_CONFIG, "kafka130:9092,kafka131:9093,kafka132:9094");
        //指定对应的key和value的序列化类型
        properties.put(ProducerConfig.KEY_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
        properties.put(ProducerConfig.VALUE_SERIALIZER_CLASS_CONFIG, StringSerializer.class.getName());
//        发送
        //1.创建 kafka 生产者对象
        KafkaProducer kafkaProducer = new KafkaProducer<String, String>(properties);
        //2.发送数据
        for (int i = 0; i < 5; i++) {
            kafkaProducer.send(new ProducerRecord<>("firstTopic", "atguigu" + i)).get();
        }
        //3.关闭资源
        kafkaProducer.close();
    }
}